//
//  GameHelper.m
//  Unity-iPhone
//
//  Created by YunQue on 2025/4/25.
//

#import <Foundation/Foundation.h>
#import "GameHelper.h"
#import "TencentQQ.h"
#import "TencentWX.h"

@implementation GameHelper


extern "C"
{
    // ios发送消息到unity
    extern void _SendPlatformMessageToUnity(int iMsgId, int iParam1, int iParam2, int iParam3, NSString *strParam1,NSString *strParam2,NSString *strParam3)
    {
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithObjectsAndKeys:
                                    [NSNumber numberWithInt:iMsgId], @"iMsgId",
                                    [NSNumber numberWithInt:iParam1], @"iParam1",
                                    [NSNumber numberWithInt:iParam2], @"iParam2",
                                    [NSNumber numberWithInt:iParam3], @"iParam3",
                                    strParam1,@"strParam1",
                                    strParam2,@"strParam2",
                                    strParam3,@"strParam3",
                                    nil];
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:NULL];
        NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        NSLog(@"ios 向 unity发送消息id:%d, 内容：%@",iMsgId,jsonString);
        UnitySendMessage("PlatformObject", "OnMessage", jsonString.UTF8String);
    }
}


extern const int PLATFORM_MSG_QQLOGIN = 1;//QQ登陆
extern const int PLATFORM_MSG_QQLOGIOUT = 2;//QQ注销
extern const int PLATFORM_MSG_WXLOGIN = 3;//微信登陆
extern const int PLATFORM_MSG_WXLOGIOUT = 4;//微信注销

extern "C"
{

    // unity发送消息到iOS
    extern void _SendUnityMessageToPlatform(int iMsgId, int iParam1, int iParam2, int iParam3, int iParam4,char *strParam1,char *strParam2,char *strParam3,char *strParam4)
    {
        TencentQQ* tencentQQ = [TencentQQ Instance];
        TencentWX* tencentWx = [TencentWX Instance];
        
        switch (iMsgId) {
            case PLATFORM_MSG_QQLOGIN:
                [tencentQQ Login];
                break;
            case PLATFORM_MSG_QQLOGIOUT:
                [tencentQQ Logout];
                break;
            case PLATFORM_MSG_WXLOGIOUT:
                [tencentWx Logout];
                break;
            case PLATFORM_MSG_WXLOGIN:
                [tencentWx Login];
                break;
                
        }
        
    }
}


extern "C"
{
    extern char* _GetStringFromPlatform(int type)
    {
        TencentQQ* tencentQQ = [TencentQQ Instance];
        TencentWX* tencentWX = [TencentWX Instance];
        if(type == 1)//QQ是否授权有效
        {
            if([tencentQQ CheckAuthorVaild] == true)
            {
                const char* str = "true";
                char* res = (char*)malloc(strlen(str)+1);
                strcpy(res, str);
                return res;
            }
            else
            {
                const char* str = "false";
                char* res = (char*)malloc(strlen(str)+1);
                strcpy(res, str);
                return res;
            }
        }
        else if (type == 2)//QQ票据获取
        {
            NSString *qqJsonString = [tencentQQ GetAuthor];
            const char* str = [qqJsonString UTF8String];
            char* res = (char*)malloc(strlen(str)+1);
            strcpy(res, str);
            return res;
        }
        else if (type == 3)//微信是否授权有效
        {
            if([tencentWX CheckAuthorVaild] == true)
            {
                const char* str = "true";
                char* res = (char*)malloc(strlen(str)+1);
                strcpy(res, str);
                return res;
            }
            else
            {
                const char* str = "false";
                char* res = (char*)malloc(strlen(str)+1);
                strcpy(res, str);
                return res;
            }
        }
        else if (type == 4)//微信是否授权有效
        {
            NSString *qqJsonString = [tencentWX GetAuthor];
            const char* str = [qqJsonString UTF8String];
            char* res = (char*)malloc(strlen(str)+1);
            strcpy(res, str);
            return res;
        }
        return nil;
    }

}

@end
